package fudan.se.lab2.service;

import fudan.se.lab2.controller.request.GetPaperRequest;
import fudan.se.lab2.controller.request.UploadRequest;
import fudan.se.lab2.domain.Contribution;
import fudan.se.lab2.domain.Meeting;
import fudan.se.lab2.repository.ContributionRepository;
import fudan.se.lab2.repository.MeetingRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

@Service
public class ContributeService {
    private static final String ERROR = "error";
    private final MeetingRepository meetingRepository;
    private final ContributionRepository contributionRepository;

    @Autowired
    public ContributeService(MeetingRepository meetingRepository, ContributionRepository contributionRepository) {
        this.meetingRepository = meetingRepository;
        this.contributionRepository = contributionRepository;
    }

    public String uploadWork(UploadRequest request) {
        String contributor = request.getUser();
        String meetingFullName = request.getMeeting();
        String newFileTitle = request.getNewFileTitle();
        String fileAbstract = request.getFileAbstract();
        String fileName = request.getFileName();
        List<String> topics = request.getTopics();
        List<String> authorNames = request.getAuthorNames();
        List<String> authorAffiliations = request.getAuthorAffiliations();
        List<String> authorRegions = request.getAuthorRegions();
        List<String> authorEmails = request.getAuthorEmails();
        String path;
        String type = fileName.indexOf('.') != -1 ? fileName.substring(fileName.lastIndexOf('.') + 1) : null;
        if (type != null) {
            if ("PDF".equalsIgnoreCase(type)) {

                // url for deploy

//                path = "/usr/local/lab4/contribution/" + contributor + '_' + newFileTitle + '_' + fileName;

                // url for local test

                path = "/Users/wuyanjie/Desktop" + contributor + "_" + newFileTitle + "_" + fileName;

                //path = "/Users/yuzhexuan/" + contributor + "_" + newFileTitle + "_" + fileName;

                File dest = new File(path);
                if (!dest.getParentFile().exists()) {
                    dest.getParentFile().mkdir();
                }
                Contribution contribution = contributionRepository.findByContributorAndFileTitle(contributor, newFileTitle);
                if (contribution == null) {
                    Contribution newContribution = new Contribution(contributor, meetingFullName, newFileTitle, fileAbstract, fileName, topics, authorNames, authorAffiliations, authorRegions, authorEmails);
                    contributionRepository.save(newContribution);
                } else {
                    contribution.setContributor(contributor);
                    contribution.setMeetingFullName(meetingFullName);
                    contribution.setFileTitle(newFileTitle);
                    contribution.setFileAbstract(fileAbstract);
                    contribution.setFileName(fileName);
                    contribution.setTopics(topics);
                    contribution.setAuthorNames(authorNames);
                    contribution.setAuthorAffiliations(authorAffiliations);
                    contribution.setAuthorRegions(authorRegions);
                    contribution.setAuthorEmails(authorEmails);
                    contributionRepository.save(contribution);
                }
                return path;
            } else {
                return ERROR;
            }
        } else {
            return ERROR;
        }
    }

    public List<String> getMeetingTopics(String meetingFullName) {
        Meeting meeting = meetingRepository.findMeetingByFullName(meetingFullName);
        System.out.println(meeting.getTopics());
        return meeting.getTopics();
    }

    public Contribution getPaperInfo(GetPaperRequest getPaperRequest) {
        return contributionRepository.findByContributorAndFileTitle(getPaperRequest.getContributor(), getPaperRequest.getFileTitle());
    }

    public String updateWork(UploadRequest request) {
        String contributor = request.getUser();
        String meetingFullName = request.getMeeting();
        String newFileTitle = request.getNewFileTitle();
        String fileAbstract = request.getFileAbstract();
        String fileName = request.getFileName();
        String originalFileTitle = request.getOriginalFileTitle();
        List<String> topics = request.getTopics();
        List<String> authorNames = request.getAuthorNames();
        List<String> authorAffiliations = request.getAuthorAffiliations();
        List<String> authorRegions = request.getAuthorRegions();
        List<String> authorEmails = request.getAuthorEmails();
        String path;
        String type = fileName.indexOf('.') != -1 ? fileName.substring(fileName.lastIndexOf('.') + 1) : null;
        if (type != null) {
            if ("PDF".equalsIgnoreCase(type)) {
                // url for deploy
//                path = "C:/Users/surface/Desktop/" + contributor + '_' + newFileTitle + '_' + fileName;
                // url for local test
                path = "/Users/wuyanjie/Desktop" + contributor + "_" + newFileTitle + "_" + fileName;
//                path = "/Users/yuzhexuan/" + contributor + "_" + newFileTitle + "_" + fileName;

                //path = "/Users/yuzhexuan/" + contributor + "_" + newFileTitle + "_" + fileName;


                File dest = new File(path);

                if (!dest.getParentFile().exists()) {
                    dest.getParentFile().mkdir();
                }

                Contribution contribution = contributionRepository.findByContributorAndMeetingFullNameAndFileTitle(contributor, meetingFullName, originalFileTitle);
                if (contribution != null) {
                    contribution.setContributor(contributor);
                    contribution.setMeetingFullName(meetingFullName);
                    contribution.setFileTitle(newFileTitle);
                    contribution.setFileAbstract(fileAbstract);
                    contribution.setFileName(fileName);
                    contribution.setTopics(topics);
                    contribution.setAuthorNames(authorNames);
                    contribution.setAuthorAffiliations(authorAffiliations);
                    contribution.setAuthorRegions(authorRegions);
                    contribution.setAuthorEmails(authorEmails);
                    contributionRepository.save(contribution);
                    return path;
                } else {
                    return ERROR;
                }
            } else {
                return ERROR;
            }
        } else {
            return ERROR;
        }
    }
}
